package com.example.duomeitijuhe.ui.fragments;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import com.example.duomeitijuhe.R;
import com.example.duomeitijuhe.others.ActivityCollector;
import com.example.duomeitijuhe.ui.LoginActivity;
import com.example.duomeitijuhe.ui.MyselfEditActivity;
import com.example.duomeitijuhe.ui.myview.TitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Michael on 2014/10/14.
 */
public class MainActivity extends FragmentActivity {

    private ImageButton dynamicButton;
    private ImageButton notifyButton;
    private ImageButton recommendButton;
    private ImageButton sendButton;
    //private ImageButton mainToMeButton;

    private ViewPager myViewPager;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private List<Fragment> fragmentLists = new ArrayList<Fragment>();
    TitleView titleView;


    //ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.action_bar_main);
        titleView=(TitleView)findViewById(R.id.title_view);

        ActivityCollector.addActivity(this);
        initData();

        /*listView = (ListView)findViewById(R.id.dynamic_list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
        listView.setAdapter(adapter);*/
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentLists.get(i);
            }

            @Override
            public int getCount() {
                return fragmentLists.size();
            }
        };
        myViewPager.setAdapter(fragmentPagerAdapter);

        myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                initImageButton();
                dynamicButton.setImageResource(R.drawable.dynamic_selected);
                switch (i){
                    case 0:
                        dynamicButton.setImageResource(R.drawable.dynamic_selected);
                        break;
                    case 1:
                        notifyButton.setImageResource(R.drawable.notify_selected);
                        break;
                    case 2:
                        recommendButton.setImageResource(R.drawable.recommend_selected);
                        break;
                    case 3:
                        sendButton.setImageResource(R.drawable.send_selected);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    public void initImageButton(){
        dynamicButton.setImageResource(R.drawable.dynamic);
        notifyButton.setImageResource(R.drawable.notify);
        recommendButton.setImageResource(R.drawable.recommend);
        sendButton.setImageResource(R.drawable.send);

    }
    public void initData(){
        dynamicButton = (ImageButton)findViewById(R.id.dynamic_image_button);
        notifyButton = (ImageButton)findViewById(R.id.notify_image_button);
        recommendButton = (ImageButton)findViewById(R.id.recommend_image_button);
        sendButton = (ImageButton)findViewById(R.id.send_image_button);
        myViewPager = (ViewPager)findViewById(R.id.my_viewpager);
        //mainToMeButton = (ImageButton)titleView.findViewById(R.id.head_image);

        DynamicFragment dynamicFragment = new DynamicFragment();
        NotifyFragment notifyFragment = new NotifyFragment();
        RecommendFragment recommendFragment = new RecommendFragment();
        SendFragment sendFragment = new SendFragment();

        fragmentLists.add(dynamicFragment);
        fragmentLists.add(notifyFragment);
        fragmentLists.add(recommendFragment);
        fragmentLists.add(sendFragment);
    }


}
